//
//  NearByTablevIew.m
//  wtshoppingcite
//
//  Created by TW on 14-11-20.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "NearByTablevIew.h"
#import "NearTableViewCell.h"
#import "ShoppingModel.h"
#import "ShopperCell.h"
#import "ShopperModel.h"

@implementation NearByTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    
    self = [super initWithFrame:frame style:style];
    if(self){
        self.backgroundView = nil;
        self.backgroundColor = [UIColor whiteColor];
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_index == 1) {
        return 50;
    }else if (_index == 2){
        return 180;
    }
    return 99;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_index == 0) {
//         NearTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer1"];
//        if (cell == nil) {
//            cell = [[NearTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer1"];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        }
//        
//        cell.shoppingModel =self.data[indexPath.row];
//        return cell;
        
        
        ShopperCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer1"];
        if (cell == nil) {
            cell = [[ShopperCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer1"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.shopperModel =self.data[indexPath.row];
        return cell;
        
    }else if (_index == 1){
       
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer2"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer2"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, (50-22)/2, 22, 22)];
            imageView.tag = 100 + indexPath.row;
            [cell.contentView addSubview:imageView];
            
            UILabel *textLabel =[self createLabelWithFrame:CGRectMake(imageView.right+10, (50-25)/2, 120, 25) text:@"" font:14.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
            textLabel.tag = 1000 + indexPath.row;
            [cell.contentView addSubview:textLabel];
            
            UIView *seperateLine = [[UIView alloc]initWithFrame:CGRectMake(3, 50 - 1, kWidth-6, 0.5)];
            seperateLine.backgroundColor = MAIN_GRAY_COLOR;
            [cell.contentView addSubview:seperateLine];
        }
        
        UIImageView *imageName = (UIImageView *)[cell.contentView viewWithTag: 100 + indexPath.row];
        imageName.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
        
        UILabel *textLabel =(UILabel *)[cell.contentView viewWithTag:1000 + indexPath.row];
        textLabel.text = self.data[indexPath.row];
        
        return cell;
        
    }else if (_index == 2){
        
        AdvertCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer3"];
        if (cell == nil) {
            cell = [[AdvertCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer3"];
            
        }
        
        cell.dataDic =self.data[indexPath.row];
        cell.advDelegate = self;
        return cell;
    }
    
    return nil;
}

-(void)selectAdvertSetcionClickWithDataDic:(NSDictionary *)dic withIndexTag:(NSInteger)index{
    
    if([self.nearbyForAdvDelegate respondsToSelector:@selector(nearBySelectAdvCellWithRowData:withIndex:)]){
        [self.nearbyForAdvDelegate nearBySelectAdvCellWithRowData:dic withIndex:index];
    }
}


-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.eventsDelegate!= nil  && [self.eventsDelegate respondsToSelector:@selector(basetableView:didSelectRowAtIndexPath:)]){
        [self.eventsDelegate basetableView:self didSelectRowAtIndexPath:indexPath];
    }
    
}


@end
